/** 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.free.i.management;

import java.lang.reflect.Method;

import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;

/** 
 * Jmx utilities
 * @author stefano
 */
public class JmxFacilities {

	//private static Logger logger = null;
	
	/*
	static{
		logger = Logger.getLogger(JmxFacilities.class);
	}
	*/
	/**
	 * Don't use, this is a facilities class
	 */
	private JmxFacilities(){
	}
	
	/**
     * Reflection analyzer to build a MBeanInfo dto from a class object 
     * @param clazz to scan
     * @param description the description to set
     * @return the MBeanInfo built
     * @throws IllegalArgumentException
     */
    @SuppressWarnings("unchecked")
	public static final MBeanInfo buildMBeanInfo(Class clazz, String description) throws IllegalArgumentException {
        
        if(clazz == null){
           return null;
        }
         
        MBeanInfo anMbeanInfo = null;
        
        //Class[] classes = clazz.getClasses();
        //Constructor[] constructors = clazz.getConstructors();    
        //Field[] fields = clazz.getFields();
        
        Method[] methods = clazz.getMethods();
        
        //Now building stuff to call MBeanInfo constructor
        MBeanOperationInfo[] someMBeanOperationInfo = new MBeanOperationInfo[methods.length];
        MBeanOperationInfo tmpMBeanOperationInfo = null;
        
        for(int iLoop=0; iLoop < methods.length; iLoop++){
            tmpMBeanOperationInfo = new MBeanOperationInfo(methods[iLoop].toGenericString(), methods[iLoop]);
            someMBeanOperationInfo[iLoop] = tmpMBeanOperationInfo;
        }
        
        anMbeanInfo = new MBeanInfo(clazz.getName(),
                                    description,
                                    null,
                                    null,
                                    someMBeanOperationInfo,
                                    null);

        return anMbeanInfo;
    }
    
    /**
     * Create an MBean server connection
     * @param registryUrl is the url of the rmi registry to connect
     * @return an MBean server connection
     * @throws JMRuntimeException
     */
    /*
    private static final MBeanServerConnection createMBeanServerConnection(String registryUrl) throws JMRuntimeException {
    	
    	
    	JMXServiceURL jmxServiceURL = null;
    	JMXConnector jmxConnector = null;
    	MBeanServerConnection mBeanServerConnection = null;
    	try{
			jmxServiceURL = new JMXServiceURL(registryUrl);
	    	
	    	jmxConnector = JMXConnectorFactory.connect(jmxServiceURL);
	    	
	    	mBeanServerConnection = jmxConnector.getMBeanServerConnection();
			
		}catch(IOException ioExc) {
			   throw (new JMRuntimeException(ioExc.getMessage()));
		}
    	
    	return mBeanServerConnection;
    }
    */
}
